6Data types, Operators, Statements, and Structures in R
6.1 Data Types in R
Data types refer to the kind of data that can be stored and manipulated within a program. In R, the basic data types include:
Numeric: Represents real numbers (e.g., 2, 15.5).
Integer: Represents whole numbers (e.g., 2L, where L denotes an integer).
Character: Represents strings (e.g., “hello”, “1234”). Character must be put between “.
Logical: Represents Boolean values (TRUE or FALSE).
6.1.1 Assigning Values and Basic Operations
Assignment Operator
The assignment operator in R is used to assign values to variables or objects in the R programming language.
The leftwards assignment operator <-: This is the most commonly used assignment operator in R. It assigns the value on its right to the object on its left. For example, x <- 3 assigns the value 3 to the variable x.
Alternative Assignment Operator (=) Apart from <-, R also supports the use of the = operator for assignments, similar to many other programming languages.
However, the use of <- is preferred in R for historical and readability reasons. For example, x = 3 is valid but x <- 3 is more idiomatic to R.
Use <- or = for assigning values, e.g., x <- 10 or x= 10
Code
x=5y=3x+y
[1] 8
Commenting Code for Clarity
Use # for comments, e.g., # This is a comment. - Comments are not executable and are used to provide relevant information about the syntax. Whatever is typed after # symbol, is considered as comment.
Code
# Multiply two variables.x=5y=3x*y
[1] 15
Arithmetic operators
In R, arithmetic operators are used to perform common mathematical operations on numbers, vectors, matrices, and arrays. Here’s an overview of the primary arithmetic operators available in R: +, -, *, /, ^
Division (/) operator - Divides the first number or vector by the second, element-wise.
Code
x=5y=3x/y
[1] 1.666667
Square (^) operator - Squares the first number by the second.
Code
x=5x^2
[1] 25
6.2 Statements
6.2.1 Logical Operations
Includes ==, !=, >, <, >=, <=.
Equality: == checks if two values are equal.
Code
# Equality 5 == 3x<-5y<-3x==y
[1] FALSE
Inequality: != checks if two values are not equal.
Code
# Inequality 5 != 3x!=y
[1] TRUE
Greater than: > checks if the value on the left is greater than the value on the right.
Code
# Greater than 5 > 3x>y
[1] TRUE
Less than: < checks if the value on the left is less than the value on the right.
Code
# Less than 5 < 3x<y
[1] FALSE
Greater than or equal to: >= checks if the value on the left is greater than or equal to the value on the right.
Code
# Greater than or equal to 5 >= 3x>=y
[1] TRUE
Less than or equal to: <= checks if the value on the left is less than or equal to the value on the right.
Code
# Less than or equal to 5 <= 3y<=x
[1] TRUE
6.3 Data Structures
Vectors
Vectors are fundamental data structures that hold elements of the same type.
They are one-dimensional arrays that can store numeric, character, or logical data.
Assigning data to vectors in R is a basic operation, essential for data manipulation and analysis.
The c() function combines values into a vector. It’s the most common method for creating vectors.